<?php

namespace App\Trip\Plane\Wrappers;

class MoreRulesResultWrapper
{
    private array $rules;

    public function __construct(array $rules)
    {
        $this->rules = $rules;
    }

    public function getCurrentRulePrice()
    {
        $merge = [];
        $curr  = [];
        foreach ($this->rules as $passenger_type => $rules_data) {
            $rules   = $rules_data['rules'];
            $current = collect($rules)->where('current', true)->first();
            if (!$current) {
                $current = collect($rules)->last();
            }
            $merge[] = $current['prices'];
            $curr    = $current;
        }

        $prices = [];
        foreach ($merge as $item) {
            foreach ($item as $pk => $pv) {
                if ($pv != 'need_merge') {
                    $prices[$pk] = $pv;
                }
            }
        }
        $curr['prices'] = $prices;
        return $curr;
    }
}